package com.blog.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.blog.constant.MsgConstant;
import com.blog.pojo.dto.ArticleDTO;
import com.blog.pojo.entity.Article;
import com.blog.pojo.entity.PageBean;
import com.blog.result.Result;
import com.blog.service.ArticleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    /**
     * 文章分页查询
     * @param pageNum
     * @param pageSize
     * @param title
     * @param categoryId
     * @param status
     * @return
     */
    @GetMapping("/page")
    public Result<PageBean> articleListPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String title,
            @RequestParam(required = false) Long categoryId,
            @RequestParam(required = false) Long status,
            @RequestParam(required = false, defaultValue = "") String searchContent
    ){
        log.info("开始文章分页查询：{}，{}，{}，{}，{}, {}", pageNum, pageSize, title, categoryId, status, searchContent);
        try {
            if(searchContent != null && !searchContent.equals("")){
                // 进行ES的高亮查询
                IPage<Article> pageResult = articleService.searchArticleHighLightPage(pageNum, pageSize, status, searchContent);
                return Result.success(new PageBean<>(pageResult.getTotal(), pageResult.getRecords()));
            }else{
                IPage<Article> pageResult = articleService.pageQuery(pageNum, pageSize, title, categoryId, status);
                return Result.success(new PageBean<>(pageResult.getTotal(), pageResult.getRecords()));
            }
        } catch (Exception e) {
            log.info("文章分页查询失败：",e);
            return Result.error(MsgConstant.ARTICLE_SELECT_ERROR);
        }
    }

    @GetMapping("/all")
    public Result<List<Article>> getArticleAll(){
        log.info("开始获取所有文章数据");
        
        try {
//            List<Article> list = articleService.list(); // 直接调用 IService 的 list() 方法
            List<Article> list = articleService.getArticleAll();
            return Result.success(list);
        } catch (Exception e) {
            log.error("获取所有文章失败：", e);
            return Result.error(MsgConstant.ARTICLE_SELECT_ERROR);
        }
    }

    /**
     * 获取文章数量
     * @return
     */
    @GetMapping("/count")
    public Result<Long> getArticleCount(){
        log.info("开始获取文章数量");

        try {
            Long count = articleService.getArticleCount();
            return Result.success(count);
        } catch (Exception e) {
            log.error("获取文章数量失败：", e);
            return Result.error(MsgConstant.ARTICLE_SELECT_ERROR);
        }
    }

    /**
     * 根据id获取单个文章的数据
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public Result<Article> getArticleDetail(@PathVariable Long id){
        log.info("开始查询单个文章信息:{}", id);

        try {
            Article article = articleService.getArticleDetail(id);
            return Result.success(article);
        } catch (Exception e) {
            return Result.error(MsgConstant.ARTICLE_SELECT_ERROR);
        }
    }

    /**
     * 添加文章
     * @param articleDTO
     * @return
     */
    @PostMapping("/add")
    public Result addArticle(@RequestBody ArticleDTO articleDTO){
        log.info("开始添加文章 {}", articleDTO);
        try {
            articleService.addArticle(articleDTO);
            return Result.success(MsgConstant.SUCCESS_MSG);
        } catch (Exception e) {
            log.info("添加失败：",e);
            return Result.error(MsgConstant.ARTICLE_ADD_ERROR);
        }
    }

    /**
     * 批量删除文章
     * @param ids
     * @return
     */
    @DeleteMapping("/batch")
    public Result deleteBatchArticle(@RequestBody List<Long> ids){
        log.info("开始批量删除数据,{}", ids);
        try {
            articleService.deleteBatchArticle(ids);
            return Result.success(MsgConstant.SUCCESS_MSG);
        } catch (Exception e) {
            return Result.error(MsgConstant.ARTICLE_DELETE_ERROR);
        }
    }

    /**
     * 删除单个文章
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public Result deleteOneArticle(@PathVariable Long id){
        log.info("开始删除单个文章：", id);

        try {
            articleService.deleteOneArticle(id);
            return Result.success(MsgConstant.SUCCESS_MSG);
        } catch (Exception e) {
            return Result.error(MsgConstant.ARTICLE_DELETE_ERROR);
        }
    }

    /**
     * 修改文章
     * @param articleDTO
     * @return
     */
    @PutMapping()
    public Result updateArticle(@RequestBody ArticleDTO articleDTO){
        log.info("开始修改文章：{}", articleDTO);

        try {
            articleService.updateArticle(articleDTO);
            return Result.success(MsgConstant.SUCCESS_MSG);
        } catch (Exception e) {
            return Result.error(MsgConstant.ARTICLE_UPDATE_ERROR);
        }
    }


}
